<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Vue中的组件</title>
		<script type="text/javascript" src="js/vue.js" ></script>
	</head>
	<body>
		<div id="root">
			<input v-model="inputValue" />
			<button @click="handlerSumbit">提交</button>
			<ul>
				<li v-for="(item, index) of list" :key="index">
					{{item}}
				</li>
			</ul>
			<ul>
				<todo-item></todo-item>
			</ul>
			<ul>
				<todo-item
				  v-for="(item, index) of list"
				  :key="index"
				  :content="item"
				  :index="index"
				  @delete="handDelete"
				>
				</todo-item>
			</ul>
		</div> 
		<script>
			Vue.component('todo-item', {
				template: '<li>item</li>'
			})
			// 局部组件,需要在实例中声明
			var TodoItem = {
				props: ['content', 'index'],// 声明接收属性
				template: '<li @click="handClick">{{content}} {{index}}</li>',
				methods: {
					handClick: function() {
						this.$emit('delete', this.index);
					}
				}
			}
			new Vue({
				el: "#root",
				// 声明局部组件
				components: {
					'todo-item': TodoItem
				},
				data: {
					inputValue: "",
					list: []
				},
				methods: {
					handlerSumbit: function() {
						this.list.push(this.inputValue)
						this.inputValue = ""
					},
					handDelete: function(index) {
						this.list.splice(index, 1)
					}
				}
			})
		</script>
	</body>
</html>
